package com.ftg.learn.sys.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ftg.learn.comm.entity.sys.User;
import com.ftg.learn.comm.entity.sys.UserRole;
import com.ftg.learn.sys.user.mapper.UserMapper;
import com.ftg.learn.sys.user.mapper.UserRoleMapper;
import com.ftg.learn.sys.user.service.IUserService;
import com.ftg.learn.sys.user.vo.UserEmpVo;
import com.ftg.learn.util.MD5;
import com.ftg.learn.util.ResBean;
import com.ftg.learn.util.ServiceUtil;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author kangjx
 * @since 2022-10-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    UserMapper userMapper;

    @Resource
    UserRoleMapper userRoleMapper;

    @Resource
    ServiceUtil serviceUtil;

    @Override
    public boolean delUserWithRole(Integer id) {
        //用户表
        int it1 = userMapper.deleteById(id);
        //用户角色中间表
        QueryWrapper<UserRole> query = new QueryWrapper<>();
        query.eq("u_id",id);
        int it2 =  userRoleMapper.delete(query);
        return it1 >=0 && it2 >= 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveUserRole(Integer id, List<Integer> rids) {
        //原来这个用户 就有大量的角色，所以我们要先删除原始的角色
        QueryWrapper<UserRole> query = new QueryWrapper<>();
        query.eq("u_id",id);
        userRoleMapper.delete(query);
        //添加全新的角色
        for (int i = 0; i < rids.size(); i++) {
            UserRole userRole = new UserRole();
            userRole.setUid(id);
            userRole.setRid(rids.get(i));
            userRoleMapper.insert(userRole);
        }
        return true;
    }

    @Override
    public ResBean accountList(int page, int limit,String val) {

        String sql = "select a.id ,u_account uaccount,emp_name empName ,emp_sex empSex" +
                "     ,emp_code empCode, d.dic_name empType,emp_job empJob ,emp_birthday empBirthday" +
                "     ,clazz_name clazzName,clazz_address clazzAddress,clazz_teacher_name clazzTeacherName, e.dic_name clazzType" +
                " from sys_user a" +
                " left join sys_employee b on a.id = b.id" +
                " left join sys_clazz c on b.clazz_id = c.id" +
                " left join (select * from sys_dictionary ) d on b.emp_type = d.id" +
                " left join (select * from sys_dictionary ) e on c.clazz_type = d.id";

        if(Objects.nonNull(val)){
            sql += " where u_account like concat('"+val+"','%') or emp_name like concat('"+val+"','%') ";
        }
        return serviceUtil.pageUtilForT(sql,page,limit,UserEmpVo.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean unFreezeUser(Integer id) {
        //首先我们要解冻的是中间表
        userRoleMapper.unFreezeRoleUserMapper(id);
        //还有我们解决是用户表
        userMapper.unFreezeUserMapper(id);
        return true;
    }

    @Override
    public boolean resetPasword(Integer id,String password) {
        String restPasswod =  "888888";
        if(!StringUtils.isNullOrEmpty(password)){
            restPasswod = password;
        }
        return userMapper.resetPasword(MD5.encrypt2ToMD5(restPasswod),id);
    }
}
